import {
	baseIo
} from "@/config/url"
import {
	toast
} from '@/utils/lib'
class Wbs {
	constructor(status) {
	    this.status=status||'close' //当前状态
	}
	initConect() {
		uni.connectSocket({
			url: baseIo
		})
		return new Promise((resolve, reject) => {
			//开启
			uni.onSocketOpen(res => {
				console.log("连接成功")
				this.status='open'
				resolve(true)
			})
			//失败回调
			uni.onSocketError(res => {
				console.log("连接失败")
				this.status='close'
				this.initConect()
				reject(false)
			})
			//关闭回调
			uni.onSocketClose(res=> {
			  console.log('连接已关闭');
			});
		})
	}
	closeConnect(){
		uni.closeSocket(res=>{
			this.status='close'
		})
	}
	send(data) {
		return new Promise((resolve, reject) => {
			uni.sendSocketMessage({
				data,
				success: res => {
					resolve(res)
				},
				fail: err => {
					console.log(err)
					reject(err)
				}
			});
		})
	}
}

export default new Wbs()
